package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @author Rick
 * @use 获取用户信息，写入本地
 *
 *
 */
public class GetCustomerInfo implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(GetCustomerInfo.class);
    private String charset = "utf-8"; //设置字符集
    private int timeout = 100000; //设置请求超时时间
    private int enableSSLflag = 1;

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        JSONObject result = new JSONObject();
        def smap, oJson, SaveResult;
        try {
            def r_customeroid = Util.filterForObject(request.getParameter("customeroid")); //客户ID
            log.debug("NGS传参:" + r_customeroid);
            //传参处理

            //列表接口调用
            if (r_customeroid.equals("")) {
                result.put("code", "-1");
                result.put("msg", "未传递参数！");
                return result
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("customeroid", r_customeroid);
            map.put("key", "ca3c64a49d1f11ecbed6d703198897725e7aba55");
            def ASCIIMD5 = KHUtil.ASCIIORDER(map);
            log.debug("接口传递参数:" + ASCIIMD5);

            oJson = CustHttpPort(eliteBean, ASCIIMD5); //调用接口方法

            log.debug("POST接口返回:" + oJson);
            return oJson;


        } catch (Exception e) {
            log.error("errormsg:", e)
            result.put("code", "-1");
            result.put("msg", "调用失败！");
            return result;
        }
    }


/**
 *
 * @use 客户详细接口调用
 * @param eliteBean
 * @param Custnameid
 * @return
 */
    private String CustHttpPort(EliteBean eliteBean, ASCIIMD5) {
        JSONObject result = new JSONObject();
        def smap, postmap, url;
        try {
            //获取接口地址
            smap = eliteBean.chaxun_sql("select target_url as url from sys_interface_config where type ='getCustomerinfo'", "dataSource");
            if (smap.size() == 0) {
                result.put("msg", "找不到对应接口");
                result.put("code", "-1");
                return result;
            } else {
                url = smap[0].getAt("url");
                log.debug("接口地址:" + url);
            };
            postmap = Util.doHttpPost(url, ASCIIMD5.toString(), charset, timeout, enableSSLflag);
            log.debug("接口返回:" + postmap);

            JSONObject obj = JSONObject.fromObject(postmap);
            if (obj.getInt("status") != 200) {
                result.put("code", "-1");
                result.put("msg", "接口调用异常");
                return result;
            } else {
                def datajson = obj.getJSONObject("data")
                //调用客户处理
                return SaveCustomer(eliteBean, datajson);
            };

        } catch (Exception e) {
            log.error("errormsg:", e)
            result.put("code", "-1");
            result.put("msg", "程序异常");
            return result;
        }
    }

    private String SaveCustomer(EliteBean eliteBean, datajson) {
        JSONObject result = new JSONObject();
        //数据处理
        def customerid       = datajson.get("customerid");
        def customername     = datajson.get("customername");
        def customeroid      = datajson.get("customeroid");
        def customertypename = datajson.get("customertypename");
        def idno             = datajson.get("idno");
        def mobile           = datajson.get("mobile");
        def sex              = datajson.get("sex");
        def address          = datajson.get("address");
        def iscompany        = datajson.get("iscompany");

        def smap, customer_guid, FDD_CUSTOMERID = "";

        try {
            smap = eliteBean.chaxun_sql("select c.customer_guid,FDD_CUSTOMERID from customer c  where c.customerid2 ='" + customeroid + "'", "dataSource");

            if (smap.size() > 0) {
                customer_guid = smap[0].get("customer_guid");
                FDD_CUSTOMERID = smap[0].get("FDD_CUSTOMERID");
                log.debug("法大大已知客户编码:" + FDD_CUSTOMERID);
                //法大大用户注册
                if (FDD_CUSTOMERID.equals("") || FDD_CUSTOMERID.equals("null")) {
                    FDD_CUSTOMERID = FDDCreateCust(eliteBean, datajson);
                    log.debug("法大大客户编码:" + FDD_CUSTOMERID);
                }
                eliteBean.update("update customer set customername='" + customername + "',customer_id='" + idno + "',gender='" + sex + "',mobile='" + mobile + "',idtype='" + customertypename + "',customerid2='" + customeroid + "',FDD_CUSTOMERID='" + FDD_CUSTOMERID + "',ADDRESS='" + address + "',CUSTOMERTYPE='" + iscompany + "' where customer_guid='" + customer_guid + "'", "dataSource");

            } else {
                customer_guid = Util.getGuid();
                FDD_CUSTOMERID = FDDCreateCust(eliteBean, datajson);
                log.debug("法大大客户编码:" + FDD_CUSTOMERID);
                eliteBean.update("insert into customer(customer_guid,customername,customerid,customer_id,gender,mobile,idtype,customerid2,createdby,createddate,FDD_CUSTOMERID,ADDRESS,CUSTOMERTYPE,STRINGFIELD9) values('" + customer_guid + "','" + customername + "','" + customerid + "','" + idno + "','" + sex + "','" + mobile + "','" + customertypename + "','" + customeroid + "','SELITE',now(),'" + FDD_CUSTOMERID + "','" + address + "','" + iscompany + "','" + customerid + "')", "dataSource");
            };

            result.put("customer_guid", customer_guid);
            return result;
        } catch (Exception e) {
            log.error("errormsg:", e)
            result.put("customer_guid", "");
            result.put("customerid", "");
            return result;
        }
    }


    private String FDDCreateCust(EliteBean eliteBean, datajson) {
        JSONObject result = new JSONObject();
        //数据处理
        def customerid = datajson.get("customerid");
        def customername = datajson.get("customername");
        def customeroid = datajson.get("customeroid");
        def customertypename = datajson.get("customertypename");
        def idno = datajson.get("idno");
        def mobile = datajson.get("mobile");
        def sex = datajson.get("sex");
        def iscompany = datajson.get("iscompany");
        def smap, url,fddjson0, fddjson = "";

        try {
            smap = eliteBean.chaxun_sql("select target_url as url from sys_interface_config where type ='apiCustomerRegist'", "dataSource");
            if (smap.size() == 0) {
                result.put("msg", "找不到对应接口");
                result.put("code", "-1");
                return result;
            } else {
                url = smap[0].getAt("url");
                log.debug("接口地址:" + url);
            };
            JSONObject jsonBody = new JSONObject();
            if(iscompany == "公司"){
                //如果是公司，先注册个人，再注册公司，传递个人为管理员
                JSONObject jsonBody0 = new JSONObject();
                jsonBody0.put("type", 1);
                jsonBody0.put("account", mobile);
                fddjson0 = Util.doHttpPost(url, jsonBody0.toString(), charset, timeout, enableSSLflag);
                JSONObject fddobj0 = JSONObject.fromObject(fddjson0);
                String Fdd_customid = fddobj0.get("data");
                log.debug("法大大个人认证ID返回:" + Fdd_customid)
                if(!Fdd_customid.equals("")){
                    jsonBody.put("type", 2);
                    jsonBody.put("account", mobile);
                    jsonBody.put("companyName", customername);
                    jsonBody.put("adminAccountId",Fdd_customid)
                }else{
                    result.put("customer_guid", "");
                    result.put("customerid", "");
                    result.put("msg", "法大大企业注册失败！");
                    return result;
                }
            }else{
                jsonBody.put("type", 1);
                jsonBody.put("account", mobile);
            }


            log.debug("法大大注册参数:" + jsonBody.toString());
            fddjson = Util.doHttpPost(url, jsonBody.toString(), charset, timeout, enableSSLflag);
            JSONObject fddobj = JSONObject.fromObject(fddjson);
            log.debug("法大大注册结果:" + fddobj);
            return fddobj.get("data");
        } catch (Exception e) {
            log.error("errormsg:", e)
            result.put("customer_guid", "");
            result.put("customerid", "");
            return result;
        }
    }


}








